﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 列车时刻查询系统
{
    public partial class regedit : Form
    {

        SqlConnection conn = null;//定义数据库引用
        public regedit()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text = textBox3.Text = "";
        }

        private void regedit_Load(object sender, EventArgs e)
        {
            string sql = "server=.;database=traintime;integrated security=true;";
            conn = new SqlConnection(sql);
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                 label4.Text = "已连接!";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = this.conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select username from users where username='" + this.textBox1.Text + "'";
            string username = (string)cmd.ExecuteScalar();
            
            if ((this.textBox1.Text == "") || (this.textBox2.Text == ""))
            {
                MessageBox.Show("用户名或密码不能为空!");
                return;
            }
            else if (username != null)
            {
                MessageBox.Show("用户名已存在，请更换!");
                return;
            }
            else if (textBox3.Text == "" || (textBox2.Text != textBox3.Text))
            {
                MessageBox.Show("密码不能为空或密码不相等！");
                return;
            }
            /*else if (username != this.textBox2.Text)
            {
                MessageBox.Show("密码错误，请重试!");
            }*/
            else
            {
                cmd.CommandText = "insert into users(username,password) values('"+textBox1.Text+"','"+textBox2.Text+"')";
                int i = cmd.ExecuteNonQuery();
                MessageBox.Show("用户 " + textBox1.Text + " 注册成功!");
                Form1 f = new Form1(this.textBox1.Text);
                this.Close();
                f.Show();
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }



    }
}
